﻿Imports System.Data.OleDb
Public Class _1Detail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        img1.Visible = False
        img_like.Visible = False

        Dim sql As String = "SELECT * FROM UserLikes WHERE Reader=@f1 AND ProfileID=@f2"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("DesignerConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", Request.Params("DesignerID"))

        Dim dr = cmd.ExecuteReader()

        If dr.HasRows Then
            'show the image
            img1.Visible = True
            btn_like.Visible = False
        Else
            'hide the image and show the button
            img1.Visible = False
            btn_like.Visible = True
        End If

        conn.Close()



    End Sub

    Protected Sub btn_comment_Click(sender As Object, e As EventArgs) Handles btn_comment.Click
        Dim sql As String = "INSERT INTO Comment (Author, Title, CommentBody, Designer_fkID) VALUES (@f1, @f2, @f3, @f4)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("DesignerConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_title.Text)
        cmd.Parameters.AddWithValue("@f3", tb_commentBody.Text)
        cmd.Parameters.AddWithValue("@f4", Request.QueryString("DesignerID"))

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("Detail.aspx?DesignerID=" & Request.QueryString("DesignerID"))
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Response.Redirect("Gallery.aspx?User_fkID=" & Request.QueryString("DesignerID"))
    End Sub

    Protected Sub btn_like_Click(sender As Object, e As EventArgs) Handles btn_like.Click
        img1.Visible = True
        btn_like.Visible = False

        Dim sql As String = "INSERT INTO UserLikes(ProfileID, Reader) VALUES (@f1, @f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("DesignerConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", Request.Params("DesignerID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
    End Sub

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("DesignerConnectionString").ConnectionString)
        conn.Open()
        Dim delPrevRatings = "DELETE FROM UserRating WHERE Rater=@f1 AND ProfileID = @f2"
        Dim cmd2 As OleDbCommand = New OleDbCommand(delPrevRatings, conn)
        cmd2.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd2.Parameters.AddWithValue("@f3", Request.Params("DesignerID"))
        cmd2.ExecuteNonQuery()



        Dim sql As String = "INSERT INTO UserRating(Rating, Rater, ProfileID) VALUES (@f1, @f2, @f3)"
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)








        cmd.Parameters.AddWithValue("@f1", Rating1.CurrentRating)
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f3", Request.Params("DesignerID"))

        




        cmd.ExecuteNonQuery()

        conn.Close()
        cmd.Dispose()
        conn.Dispose()

    End Sub

    Protected Sub ImageButton1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles btn_like.Click

        img1.Visible = True
        btn_like.Visible = False

        Dim sql As String = "INSERT INTO UserLikes(ProfileID, Reader) VALUES (@f1, @f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("DesignerConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", Request.Params("DesignerID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()

    End Sub
End Class